import 'package:im_demo/app/data/chat/api/request/add_group_request.dart';
import 'package:im_demo/app/data/chat/api/request/login_request.dart';
import 'package:im_demo/app/data/chat/api/request/test_post_request.dart';
import 'package:im_demo/app/data/chat/api/response/add_group_response.dart';
import 'package:im_demo/app/data/chat/api/response/base_response.dart';
import 'package:im_demo/app/data/chat/api/response/test_post_response.dart';
import 'package:im_demo/utils/logger_util.dart';

import '../../../../core/network/http_util.dart';

class Api {
  // Future<HttpResult<AddGroupResponse>> addGroup(AddGroupRequest request) async {
  //   final resp = await HttpUtil().post<AddGroupResponse>(
  //     '/api/chat/group/addGroup',
  //     data: request.toJson(),
  //   );
  //   return resp;
  // }

  Future<HttpResult<AddGroupResponse>> addGroup(AddGroupRequest request) async {
    final result = await HttpUtil().post<Map<String, dynamic>>(
      '/api/chat/group/addGroup',
      data: request.toJson(),
    );
    logE('addGroup()-->$result');
    return _handleResult<AddGroupResponse>(
      result,
      (data) => AddGroupResponse.fromJson(data),
    );
  }

  Future<HttpResult<TestPostResponse>> testPost(TestPostRequest request) async {
    final result = await HttpUtil().post<Map<String, dynamic>>(
      '/posts',
      baseUrl: 'https://jsonplaceholder.typicode.com',
      data: request.toJson(),
    );
    logE('testPost()-->$result');
    return _handleResult<TestPostResponse>(
      result,
      (data) => TestPostResponse.fromJson(data),
    );
  }

  Future<HttpResult<BaseResponse>> getSMSCode(String phoneNumber) async {
    final result = await HttpUtil().post<Map<String, dynamic>>(
      '/api/sms/send-code',
      params: {"phone": phoneNumber},
    );
    logE('getSMSCode()-->$result');
    return _handleResult<BaseResponse>(
      result,
      (data) => BaseResponse.fromJson(data),
    );
  }

  Future<HttpResult<BaseResponse>> login(LoginRequest request) async {
    final result = await HttpUtil().post<Map<String, dynamic>>(
      '/api/auth/login',
      data: request.toJson(),
    );
    logE('login()-->$result');

    return _handleResult<BaseResponse>(
      result,
      (data) => BaseResponse.fromJson(data),
    );
  }

  Future<HttpResult<BaseResponse>> getUserInfo() async {
    final result = await HttpUtil().post<Map<String, dynamic>>(
      '/api/user/getUser',
    );
    logE('getUser()-->$result');

    return _handleResult<BaseResponse>(
      result,
      (data) => BaseResponse.fromJson(data),
    );
  }

  static HttpResult<T> _handleResult<T>(
    HttpResult<Map<String, dynamic>> result,
    T Function(Map<String, dynamic>) fromJson,
  ) {
    if (result.isSuccess) {
      try {
        final data = fromJson(result.data!);
        return HttpResult.success(data);
      } catch (e) {
        return HttpResult.failure(HttpError(code: -9999, message: '数据解析失败'));
      }
    } else {
      return HttpResult.failure(result.error!);
    }
  }
}
